package org.gordonframework.site.forms.elements.formats {
	import org.gordonframework.site.forms.elements.GButton;	
	import org.gordonframework.core.collection.GItemWrapper;
	import org.gordonframework.core.collection.GSet;
	import org.gordonframework.site.forms.elements.GCheckBox;
	import org.gordonframework.site.forms.elements.GElement;
	import org.gordonframework.site.forms.elements.GTextInput;		

	/**
	 * @author eriarv
	 */
	public class GElementSkinFormatFactory {
		
		private static var instance : GElementSkinFormatFactory = null;
		
		private var textFormats : GSet;
		private var skinFormats : GSet;
		
		
		public function GElementSkinFormatFactory( caller : String ) {
			if ( caller != "Alright" ) {
				throw new Error( "Please use static getInstance()");
			}
			
			textFormats = new GSet();
			skinFormats = new GSet();
			
			setDefaultFormats();
		}
		
		private function setDefaultFormats() : void {
			var textInputSkinFormat : Object = {
				background:{
					width:150,
					height:20,
					color:0x777777,
					alpha:1},
				focus:{
					color:0x191919},
				border:{
					thickness:1,
					color:0xbbbbbb,
					alpha:1},
				paddingX:5,
				paddingY:2,
				focus:{
					color:0xff0000
				}
			};
			setSkinFormatForType(GTextInput.TYPE, textInputSkinFormat);
			
			
			var textInputTextFormat : Object = {
				label:{ font:"Arial", size:11, color:0xffffff},
				input:{font:"Arial", size:12, color:0xffffff},
				help:{font:"Arial", size:10, color:0xffffff}
			};
			setTextFormatForType(GTextInput.TYPE, textInputTextFormat);
			
			setSkinFormatForType(GElement.TYPE, {invalidInputColor:0xa82424});
			
			var checkBoxSkinFormat : Object = {
				boxOn:{
					background:{
						width:11,
						height:11,
						color:0xffffff
					},
					border:{
						thickness:1,
						color:0x4e4747,
						alpha:1
					},
					marker:{
						thickness:1.5,
						color:0x21a121,
						alpha:1
					}
				},
				boxOff:{
					background:{
						width:11,
						height:11,
						color:0xffffff
					},
					border:{
						thickness:1,
						color:0x4e4747,
						alpha:1
					}
				},
				paddingX:5,
				paddingY:-2,
				focus:{
					color:0xff0000
				}
			};
			setSkinFormatForType(GCheckBox.TYPE, checkBoxSkinFormat);
			setTextFormatForType(GCheckBox.TYPE, 
				{
					label: {font:"Arial", size:12, color:0xffffff}
				}
			);
			
			
			setTextFormatForType(GButton.TYPE, 
				{
					label: {font:"Arial", size:12, color:0xffffff}
				}
			);
			setSkinFormatForType(GButton.TYPE, 
				{
					background:{
						width:150,
						height:20,
						color:0x777777,
						alpha:1},
					border:{
						thickness:1,
						color:0xbbbbbb,
						alpha:1},
					paddingX:5,
					paddingY:2,
					focus:{
						color:0xff0000
					}
				}
			);
			
			setSkinFormatForType(GElement.TYPE, 
				{
					focus:{
						color:0xff0000
					}
				}
			);
		}

		
		
		public function getTextFormatForType( type : String ) : Object {
			return ( textFormats.getItemById(type) as GItemWrapper ).object;
		}
		
		/**
		 * Sets the textformat of element of type: type, replacing any
		 * existing format. Format object specifics depends on the used Skin class
		 * 
		 * @param type - type of element
		 * @param format - format object
		 */
		public function setTextFormatForType( type : String, format : Object ) : Boolean {
			return textFormats.addAndReplace(new GItemWrapper(type, format ) );
		}
		
		public function getSkinFormatForType( type : String ) : Object {
			return ( skinFormats.getItemById(type) as GItemWrapper ).object;
		}
		
		/**
		 * Sets the skin format of element of type: type, replacing any
		 * existing format. Format object specifics depends on the used Skin class
		 * 
		 * @param type - type of element
		 * @param format - format object
		 */
		public function setSkinFormatForType( type : String, format : Object ) : Boolean {
			return skinFormats.addAndReplace(new GItemWrapper(type, format ) );
		}
		
		
		public static function getInstance() : GElementSkinFormatFactory {
			
			if ( instance == null ) {
				instance = new GElementSkinFormatFactory( "Alright" );
			}
			
			return instance;
		}
	}
}
